﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class TagModule
    {
        public int Add(KeRui.Model.TagModule model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModuleID", SqlDbType.Int, 4), new SqlParameter("@ModuleName", SqlDbType.NVarChar, 200), new SqlParameter("@DataTable", SqlDbType.VarChar, 50), new SqlParameter("@TopNum", SqlDbType.Int, 4), new SqlParameter("@StrWhere", SqlDbType.NText), new SqlParameter("@OrderBy", SqlDbType.VarChar, 200), new SqlParameter("@Direction", SqlDbType.VarChar, 50), new SqlParameter("@Code", SqlDbType.NText) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.ModuleName;
            parameters[2].Value = model.DataTable;
            parameters[3].Value = model.TopNum;
            parameters[4].Value = model.StrWhere;
            parameters[5].Value = model.OrderBy;
            parameters[6].Value = model.Direction;
            parameters[7].Value = model.Code;
            DbHelperSQL.RunProcedure("TagModule_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public void Delete(int ModuleID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModuleID", SqlDbType.Int, 4) };
            parameters[0].Value = ModuleID;
            DbHelperSQL.RunProcedure("TagModule_Delete", parameters, out num);
        }

        public bool Exists(string moduleName)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModuleName", SqlDbType.NVarChar, 200) };
            parameters[0].Value = moduleName;
            return (DbHelperSQL.RunProcedure("TagModule_Exists", parameters, out num) == 1);
        }

        public DataTable GetDBList()
        {
            SqlParameter[] parameters = new SqlParameter[0];
            return DbHelperSQL.RunProcedure("TagModule_GetDBList", parameters, "ds").Tables[0];
        }

        public DataTable GetFieldsByTableID(int tableID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@TableID", SqlDbType.Int) };
            parameters[0].Value = tableID;
            return DbHelperSQL.RunProcedure("TagModule_GetFieldsByTableID", parameters, "ds").Tables[0];
        }

        public KeRui.Model.TagModule GetModel(int ModuleID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModuleID", SqlDbType.Int, 4) };
            parameters[0].Value = ModuleID;
            KeRui.Model.TagModule module = new KeRui.Model.TagModule();
            DataSet set = DbHelperSQL.RunProcedure("TagModule_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ModuleID"].ToString() != "")
                {
                    module.ModuleID = int.Parse(set.Tables[0].Rows[0]["ModuleID"].ToString());
                }
                module.ModuleName = set.Tables[0].Rows[0]["ModuleName"].ToString();
                module.DataTable = set.Tables[0].Rows[0]["DataTable"].ToString();
                if (set.Tables[0].Rows[0]["TopNum"].ToString() != "")
                {
                    module.TopNum = int.Parse(set.Tables[0].Rows[0]["TopNum"].ToString());
                }
                module.StrWhere = set.Tables[0].Rows[0]["StrWhere"].ToString();
                module.OrderBy = set.Tables[0].Rows[0]["OrderBy"].ToString();
                module.Direction = set.Tables[0].Rows[0]["Direction"].ToString();
                module.Code = set.Tables[0].Rows[0]["Code"].ToString();
                return module;
            }
            return null;
        }

        public DataTable GetModelByName(string moduleName)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModuleName", SqlDbType.NVarChar, 200) };
            parameters[0].Value = moduleName;
            return DbHelperSQL.RunProcedure("TagModule_GetModelByName", parameters, "ds").Tables[0];
        }

        public string GetTableIDByTableName(string tableName)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@TableName", SqlDbType.VarChar, 0xff) };
            parameters[0].Value = tableName;
            return DbHelperSQL.RunProcedure("TagModule_GetTableIDByTableName", parameters, "ds").Tables[0].Rows[0]["id"].ToString();
        }

        public string GetTableNameByTableID(int tableID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@TableID", SqlDbType.Int) };
            parameters[0].Value = tableID;
            return DbHelperSQL.RunProcedure("TagModule_GetTableNameByTableID", parameters, "ds").Tables[0].Rows[0]["name"].ToString();
        }

        public void Update(KeRui.Model.TagModule model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModuleID", SqlDbType.Int, 4), new SqlParameter("@ModuleName", SqlDbType.NVarChar, 200), new SqlParameter("@DataTable", SqlDbType.VarChar, 50), new SqlParameter("@TopNum", SqlDbType.Int, 4), new SqlParameter("@StrWhere", SqlDbType.NText), new SqlParameter("@OrderBy", SqlDbType.VarChar, 200), new SqlParameter("@Direction", SqlDbType.VarChar, 50), new SqlParameter("@Code", SqlDbType.NText) };
            parameters[0].Value = model.ModuleID;
            parameters[1].Value = model.ModuleName;
            parameters[2].Value = model.DataTable;
            parameters[3].Value = model.TopNum;
            parameters[4].Value = model.StrWhere;
            parameters[5].Value = model.OrderBy;
            parameters[6].Value = model.Direction;
            parameters[7].Value = model.Code;
            DbHelperSQL.RunProcedure("TagModule_Update", parameters, out num);
        }
    }
}

